//
// Created by jwhww on 25-11-6.
//

#include "AllHeadFile.h"

//---------------主板can相关变量初始化-----------------
//Tx相关
CAN_TxHeaderTypeDef TxHeader_CBoard_toUP; //向上面主板发报
CAN_TxHeaderTypeDef TxHeader_CBoard_toDOWN; //下面主板发报
uint32_t Tx_mailbox_toUP = 0; //发送邮箱
uint32_t Tx_mailbox_toDOWN = 0; //发送邮箱
uint8_t can_send_content_CBoard[8] = {0};           // CAN发送数据内容
//Rx相关
// CAN_RxHeaderTypeDef RxHeader_CBoard; //主板接收消息头结构体，一个can总线只需要初始化一个
CBoard_Info CBoard_Info_low;
//---------------主板can相关变量初始化-----------------


//------------------------------------------------------------------------------------------------------
// 函数简介     C板沟通初始化
// 使用示例     CBoard_connect_Init();
// 输入参数
// 返回参数     无
// 备注信息     无
//------------------------------------------------------------------------------------------------------
void CBoard_connect_Init(void)
{
    // 初始化CAN
    TxHeader_CBoard_toDOWN.IDE = CAN_ID_STD;
    TxHeader_CBoard_toDOWN.RTR = CAN_RTR_DATA;
    TxHeader_CBoard_toDOWN.ExtId = 0x00000000;
    TxHeader_CBoard_toDOWN.DLC = 8;
    TxHeader_CBoard_toDOWN.TransmitGlobalTime = DISABLE;

    TxHeader_CBoard_toUP.IDE = CAN_ID_STD;
    TxHeader_CBoard_toUP.RTR = CAN_RTR_DATA;
    TxHeader_CBoard_toUP.ExtId = 0x00000000;
    TxHeader_CBoard_toUP.DLC = 8;
    TxHeader_CBoard_toUP.TransmitGlobalTime = DISABLE;
}

//------------------------------------------------------------------------------------------------------
// 函数简介     C板沟通-报文装载
// 使用示例     CBoard_connect_Load();
// 输入参数     speed_actual_X          X轴速度
// 输入参数     speed_actual_Y          Y轴速度
// 输入参数     speed_actual_Z          Z轴速度
// 输入参数     power_actual_x100       允许功率百分比x100
// 返回参数     无
// 备注信息     并非完全体，还要加入很多兼容性，目前只兼容舵轮底盘
//------------------------------------------------------------------------------------------------------
void CBoard_connect_Load(int16_t speed_set_X, int16_t speed_set_Y, int16_t speed_set_Z, int16_t power_allowable_percentage)
{
    // 装载CAN发送数据内容(小端序）
    can_send_content_CBoard[0] = speed_set_X & 0x00ff;
    can_send_content_CBoard[1] = speed_set_X >> 8;
    can_send_content_CBoard[2] = speed_set_Y & 0x00ff;
    can_send_content_CBoard[3] = speed_set_Y >> 8;
    can_send_content_CBoard[4] = speed_set_Z & 0x00ff;
    can_send_content_CBoard[5] = speed_set_Z >> 8;
    can_send_content_CBoard[6] = power_allowable_percentage & 0x00ff;
    can_send_content_CBoard[7] = power_allowable_percentage >> 8;
}

//------------------------------------------------------------------------------------------------------
// 函数简介     C板沟通-收报
// 使用示例     CBoard_connect_Receive(&hcan1);
// 输入参数     hcan                    CAN句柄
// 返回参数     无
// 备注信息     无
//------------------------------------------------------------------------------------------------------
void CBoard_connect_Receive(const uint8_t* rx_data) {
    CBoard_Info_low.can_id = RxHeader_CAN1.StdId;
    CBoard_Info_low.speed_actual_X = (int16_t)(((int16_t)rx_data[1] << 8) | ((int16_t)rx_data[0]));
    CBoard_Info_low.speed_actual_Y = (int16_t)(((int16_t)rx_data[3] << 8) | ((int16_t)rx_data[2]));
    CBoard_Info_low.speed_actual_Z = (int16_t)(((int16_t)rx_data[5] << 8) | ((int16_t)rx_data[4]));
    CBoard_Info_low.power_actual_x100 = (int16_t)(((int16_t)rx_data[7] << 8) | ((int16_t)rx_data[6]));
}
//------------------------------------------------------------------------------------------------------
// 函数简介     C板沟通-发报
// 使用示例     CBoard_connect_Send();
// 输入参数
// 返回参数     无
// 备注信息     无
//------------------------------------------------------------------------------------------------------
void CBoard_connect_SendtoDOWN(CAN_HandleTypeDef *hcan) {
    TxHeader_CBoard_toDOWN.StdId = 0x111;
    for (uint8_t i = 0; i < 10; i++) {                   //发信失败则再试9次
        if (HAL_CAN_IsTxMessagePending(hcan, Tx_mailbox_toDOWN)==RESET) {    //判断目标缓冲区是否为空
            if (HAL_OK == HAL_CAN_AddTxMessage(hcan,&TxHeader_CBoard_toDOWN,can_send_content_CBoard,&Tx_mailbox_toDOWN))
                break;
        }
    }
}